﻿#pragma once


#include "Render/Scene/Frustum.h"
#include "Render/Scene/SceneParametersBuffer.h"
#include "Render/ForegroundMesh/ForegroundMeshInstances.h"
#include "Render/PreRender/PreRenderWidgets.h"
#include "Render/Scene/VulkanScene.h"

class CVulkanWindow;
class CDefferRenderAttachments;
class CAntiAliasingAttachments;
class CVulkanPixelObjectGroup;

class CVulkanViewportWidget
{
public:
    CVulkanViewportWidget() = default;
    CVulkanViewportWidget(CVulkanViewportWidget&& Other)
    {
        //防止在复制对象时复制指针
        this->Attachments = Other.Attachments;
        this->AntiAliasingAttachments = Other.AntiAliasingAttachments;
        Other.Attachments = nullptr;
        Other.AntiAliasingAttachments = nullptr;
    }

    ~CVulkanViewportWidget();

    void BindData(const SPreRenderViewportWidget& InData);
   
    void Draw(CVulkanWindow *InSurface ,vk::CommandBuffer CommandBuffer);

    CDefferRenderAttachments* Attachments=nullptr;
    CAntiAliasingAttachments* AntiAliasingAttachments=nullptr;
    CVulkanPixelObjectGroup* PixelObjectGroup=nullptr;
protected:
    void CalculateMathView();

public:
    CVulkanScenes ScenePainter;
    CVulkanForegroundMeshInstances ForegroundMeshPainter;
    
    SPreRenderViewportWidget Data;
    //由渲染数据推导出的视口数据
    vk::Extent2D ViewportSize;
    SMatrix4x4 ProjectionMatrix;
    SMatrix4x4 ViewMatrix;
    SFrustumPoints FrustumPoints;
    SSceneParameters Parameters;
    
    //粗略计算球体是否与视椎体相交，有可能会将不想交的球体误判为相交
    bool FastIsSphereIntersectFrustum(const SVec3& InCenter,float InRadius);
};

//绘制所有的视口控件
class CVulkanViewportWidgets
{
public:
    ~CVulkanViewportWidgets();

    void PreProcess(const TVector<SPreRenderViewportWidget>& AllViewportData);

    void CreateSelfRecordTasks(CVulkanWindow *InRenderSurface
        ,SRenderCommandBuffers &OutCmdBuffersToExecute);
protected:
    TVector<CVulkanViewportWidget> ViewportWidgets;

    vk::CommandBuffer CommandBuffer;
};
